﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Model;
using BLL;
using System.Data;
using System.Reflection;

namespace OA.Controllers
{
    public class ProduceController : Controller
    {
        //
        // GET: /Produce/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ProduceInfo()
        {
            return View();
        }

        //获取模板名
        public ActionResult getTepName()
        {
            goodsType gt = new goodsType();
            goodsTypeManager gtManager = new goodsTypeManager();
            List<goodsType> Names = gtManager.GetAll().ToList();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Names.Count(); i++)
            {
                sb.Append("<li class=\"tep_lis\" id="+Names[i].id+" onmouseover=\"tep_Over(this)\" onmouseout=\"tep_Out(this)\" onclick=\"tep_Click(this)\">" + Names[i].typeName + "</li>");
            }
            return Content(sb.ToString());
        }
        //加载商品
        public ActionResult GetGoods(FormCollection fc)
        {
            goods gs = new goods();
            goodsManager gsManager = new goodsManager();
            goodsType gt = new goodsType();
            goodsTypeManager gtManager = new goodsTypeManager();
            List<goods> gsList = null;
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            string strId = fc["id"];
            if (string.IsNullOrEmpty(strId))
            {
                gsList = gsManager.GetAll().ToList();
            }
            else
            {
                gsList=gsManager.getByGoodsTypeId(strId).ToList();
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < gsList.Count(); i++)
            {
                if(i<pageSize)
                {
                    sb.Append("<tr>");
                    sb.Append("<td>"+gsList[i].goodsId+"</td>");
                    sb.Append("<td>" + gtManager.GetByID(Convert.ToInt32(gsList[i].goodsTypeId)).typeName + "</td>");
                    sb.Append("<td>" + gsList[i].goodsName + "</td>");
                    sb.Append("<td>" + gsList[i].unit + "</td>");
                    sb.Append("<td>" + gsList[i].price + "</td>");
                    sb.Append("<td>" + gsList[i].Promotionprice+ "</td>");
                    sb.Append("<td>" + gsList[i].finalCost+ "</td>");
                    if (gsList[i].Promotion == true)
                    {
                        sb.Append("<td><input type=\"checkbox\" checked=\"" + gsList[i].Promotion + "\" onclick=\" return false\"></td>");
                    }
                    else
                    {
                        sb.Append("<td><input type=\"checkbox\" onclick=\" return false\"></td>");
                    }
                    sb.Append("<td>" + gsList[i].amount + "</td>");
                    sb.Append("</tr>");
                }
                else
                {
                    break;
                }
                
            }
                return Content(sb.ToString());
        }

        //实现分页
        public ActionResult newPage(FormCollection fc)
        {
            goods gs = new goods();
            goodsManager gsManager = new goodsManager();
            goodsType gt = new goodsType();
            goodsTypeManager gtManager = new goodsTypeManager();
            int pageNum = Convert.ToInt32(fc["pageNum"]);
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            int maxPage = pageNum * pageSize - 1;
            int minPage = maxPage - pageSize + 1;
            List<goods> gsList = null;
            string strId = fc["id"];
            if (string.IsNullOrEmpty(strId))
            {
                gsList = gsManager.GetAll().ToList();
            }
            else
            {
                gsList = gsManager.getByGoodsTypeId(strId).ToList();
            }
            StringBuilder sb = new StringBuilder();
            for (int i = minPage; i <= maxPage; i++)
            {
                try
                {
                    sb.Append("<tr ondblclick=\"editGoods(" + gsList[i].id + ")\" onclick=\"selectGoods(" + gsList[i].goodsId+ ")\" onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                    sb.Append("<td>" + gsList[i].goodsId + "</td>");
                    sb.Append("<td>" + gtManager.GetByID(Convert.ToInt32(gsList[i].goodsTypeId)).typeName + "</td>");
                    sb.Append("<td>" + gsList[i].goodsName + "</td>");
                    sb.Append("<td>" + gsList[i].unit + "</td>");
                    sb.Append("<td>" + gsList[i].price + "</td>");
                    sb.Append("<td>" + gsList[i].Promotionprice + "</td>");
                    sb.Append("<td>" + gsList[i].finalCost + "</td>");
                    if (gsList[i].Promotion == true)
                    {
                        sb.Append("<td><input type=\"checkbox\" checked=\"" + gsList[i].Promotion + "\" onclick=\" return false\"></td>");
                    }
                    else
                    {
                        sb.Append("<td><input type=\"checkbox\" onclick=\" return false\"></td>");
                    }
                    sb.Append("<td>" + gsList[i].amount + "</td>");
                    sb.Append("<td><a href=\"#\" onclick=\"editGoods(" + gsList[i].id + ")\">编 辑</a>&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"delGoods(" + gsList[i].id + ")\">删 除</a></td>");
                    sb.Append("</tr>");
                }
                catch
                {
                    break;
                }
            }
            return Content(sb.ToString());
        }

        //最大页数
        public ActionResult Data_Load_maxPage(FormCollection fc)
        {
            goods gs = new goods();
            goodsManager gsManager = new goodsManager();
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            UsersInfoManager UM = new UsersInfoManager();
            List<goods> gsList = null;
            string strId = fc["id"];
            if (string.IsNullOrEmpty(strId))
            {
                gsList = gsManager.GetAll().ToList();
            }
            else
            {
                gsList = gsManager.getByGoodsTypeId(strId).ToList();
            }
            int count = gsList.Count();
            string oop = Math.Ceiling((double)count / pageSize).ToString();
            return Content(oop);
        }

       
        //添加商品
        public ActionResult AddGoods()
        {
            goodsType gt = new goodsType();
            goodsTypeManager gtManager = new goodsTypeManager();
            List<goodsType> list = gtManager.GetAll().ToList();
            var selectList = new SelectList(list,"id","typeName");
            List<SelectListItem> select = new List<SelectListItem>();
            select.Add(new SelectListItem { Value = "千克", Text = "千克" });
            select.Add(new SelectListItem { Value = "瓶", Text = "瓶" });
            select.Add(new SelectListItem { Value = "包", Text = "包" });
            select.Add(new SelectListItem { Value = "袋", Text = "袋" });
            select.Add(new SelectListItem { Value = "支", Text = "支" });
            ViewData["goodsType"] = selectList;
            ViewData["unit"] = select;
            return View();
        }
        [HttpPost]
        public ActionResult addGoods( FormCollection fc)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                goods gs = new goods();
                goodsManager gsManager = new goodsManager();
                goodsflowing gf = new goodsflowing();
                goodsflowingManager gfManager = new goodsflowingManager();
                goodsTypeManager gtManager = new goodsTypeManager();
                string goodsId = fc["goodsId"];
                string goodsName = fc["goodsName"];
                string goodsTypeId = fc["goodsType"];
                string unit = fc["unit"];
                string finalCost = fc["finalCost"];
                string price = fc["price"];
                bool promotion = Convert.ToBoolean(fc["radio"]);
                int amount = Convert.ToInt32(fc["amount"]);
                List<string> listId = gsManager.getgoodsId().ToList();
                for (int i = 0; i < listId.Count; i++)
                {
                    if (listId[i] == goodsId)
                    {
                        return Content("<script>alert('商品编号已存在')</script>");
                    }

                }
                gs.goodsId = goodsId;
                gs.goodsName = goodsName;
                gs.goodsTypeId = goodsTypeId;
                gs.unit = unit;
                gs.finalCost = finalCost;
                gs.price = price;
                gs.Promotion = promotion;
                gs.amount = amount;
                gs.Promotionprice = price;
                gsManager.Add(gs);
                gsManager.Update();
                string name = System.Web.HttpContext.Current.User.Identity.Name;
                gf.username = name;
                gf.Time = DateTime.Now;
                gf.operate = "添加商品";
                gf.goodsId = gs.goodsId;
                gf.goodsName = gs.goodsName;
                gf.unit = gs.unit;
                gf.price = Convert.ToDecimal(gs.price);
                gf.finalCost = Convert.ToDecimal(gs.finalCost);
                gf.amount = gs.amount;
                gf.goodsTypeId = gtManager.GetByID(Convert.ToInt32(goodsTypeId)).typeName;
                gfManager.Add(gf);
                gfManager.Update();
                return Content("<script>alert('添加成功');window.parent.location.href='/Produce/ProduceInfo';parent.layer.close(parent.layer.getFrameIndex());</script>");
            }
            else
            {
                return Content("<script>alert('已经超时,请重新登录');window.parent.parent.location.href='/Login/Login';parent.layer.close(parent.layer.getFrameIndex());</script>");
            }
           
        }
        
        //编辑商品
        public ActionResult editGoods()
        {
            string id= System.Web.HttpContext.Current.Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                goodsType gt = new goodsType();
                goodsTypeManager gtManager = new goodsTypeManager();
                goods gs = new goods();
                goodsManager gsManager = new goodsManager();
                List<goodsType> list = gtManager.GetAll().ToList();
                gs = gsManager.GetByID(Convert.ToInt32(id));
                List<SelectListItem> select = new List<SelectListItem>();
                select.Add(new SelectListItem { Value = gs.unit, Text =gs.unit, Selected = true });
                if (gs.unit != "千克")
                {
                     select.Add(new SelectListItem { Value = "千克", Text = "千克" });
                }
                if (gs.unit != "瓶")
                {
                    select.Add(new SelectListItem { Value = "瓶", Text = "瓶" });
                }
                if (gs.unit != "包")
                {
                    select.Add(new SelectListItem { Value = "包", Text = "包" });
                }
                if (gs.unit != "袋")
                {
                    select.Add(new SelectListItem { Value = "袋", Text = "袋" });
                }
                if (gs.unit != "支")
                {
                    select.Add(new SelectListItem { Value = "支", Text = "支" });
                }
                ViewData["goodsId"] = gs.goodsId;
                ViewData["goodsName"] = gs.goodsName;
                var selectList = new SelectList(list, "id", "typeName", gs.goodsTypeId.Trim());
                ViewData["goodsType"] = selectList;
                ViewData["unit"] = select;
                ViewData["finalCost"] = gs.finalCost;
                ViewData["price"] = gs.price;
                ViewData["radio"] = gs.Promotion;
                ViewData["amount"] = gs.amount;
                return View();
            }
            return View();
        }

        [HttpPost]
        public ActionResult editGoods(FormCollection fc)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                goods gs = new goods();
                goodsManager gsManager = new goodsManager();
                goodsflowing gf = new goodsflowing();
                goodsflowingManager gfManager = new goodsflowingManager();
                goodsTypeManager gtManager = new goodsTypeManager();
                string goodsId = fc["goodsId"];
                string goodsName = fc["goodsName"];
                string goodsTypeId = fc["goodsType"];
                string unit = fc["unit"];
                string finalCost = fc["finalCost"];
                string price = fc["price"];
                bool promotion = Convert.ToBoolean(fc["radio"]);
                //  int amount = Convert.ToInt32(fc["amount"]);
                gs = gsManager.getBygoodsId(goodsId);
                gs.goodsName = goodsName;
                gs.goodsTypeId = goodsTypeId;
                gs.unit = unit;
                gs.finalCost = finalCost;
                gs.price = price;
                gs.Promotion = promotion;
                gs.Promotionprice = price;
                gsManager.Update();
                string name = System.Web.HttpContext.Current.User.Identity.Name;
                gf.username = name;
                gf.Time = DateTime.Now;
                gf.operate = "编辑商品";
                gf.goodsId = gs.goodsId;
                gf.goodsName = gs.goodsName;
                gf.unit = gs.unit;
                gf.price = Convert.ToDecimal(gs.price);
                gf.finalCost = Convert.ToDecimal(gs.finalCost);
                gf.amount = gs.amount;
                gf.goodsTypeId = gtManager.GetByID(Convert.ToInt32(goodsTypeId)).typeName;
                gfManager.Add(gf);
                gfManager.Update();
                return Content("<script>alert('更新成功');window.parent.location.href='/Produce/ProduceInfo';parent.layer.close(parent.layer.getFrameIndex());</script>");
            }
            else
            {
                return Content("<script>alert('已经超时,请重新登录');window.parent.parent.location.href='/Login/Login';parent.layer.close(parent.layer.getFrameIndex());</script>");
            }
        }
       
        //删除商品
        public ActionResult delGoods(FormCollection fc)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                string id = fc["Id"];
                goods gs = new goods();
                goodsManager gsManager = new goodsManager();
                goodsflowing gf = new goodsflowing();
                goodsflowingManager gfManager = new goodsflowingManager();
                if (!string.IsNullOrEmpty(id))
                {
                    gs = gsManager.GetByID(Convert.ToInt32(id)); 
                    string name = System.Web.HttpContext.Current.User.Identity.Name;
                    gf.username = name;
                    gf.Time = DateTime.Now;
                    gf.operate = "删除商品";
                    gf.goodsId = gs.goodsId;
                    gf.goodsName = gs.goodsName;
                    gf.unit = gs.unit;
                    gf.price = Convert.ToDecimal(gs.price);
                    gf.finalCost = Convert.ToDecimal(gs.finalCost);
                    gf.amount = gs.amount;
                    gf.goodsTypeId = gs.goodsTypeId;
                    gsManager.delGoods(Convert.ToInt32(id));
                    gfManager.Add(gf);
                    gfManager.Update();
                    return Content("删除成功");
                }

            }
            else
            {
                return Content("<script>alert('已经超时,请重新登录');window.parent.location.href='/Login/Login';</script>");
            }

            return View();
        }

        //进货或退货
        public ActionResult goodsOption()
        {
            string option = System.Web.HttpContext.Current.Request.QueryString["option"];
            string goodsId = System.Web.HttpContext.Current.Request.QueryString["goodsId"];
            if (!string.IsNullOrEmpty(option))
            {
                if (!string.IsNullOrEmpty(goodsId))
                    {
                        goods gs = new goods();
                        goodsManager gsManager = new goodsManager();
                        gs = gsManager.getBygoodsId(goodsId);
                        ViewData["goodsId"] = gs.goodsId;
                        ViewData["goodsName"] = gs.goodsName;
                        ViewData["finalCost"] = gs.finalCost;
                        ViewData["price"] = gs.price;
                        ViewData["oldAmount"] = gs.amount;
                        return View();
                    }
               
                
            }
            return View();
        }

        [HttpPost]
        public ActionResult goodsOption( FormCollection fc)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                string option = System.Web.HttpContext.Current.Request.QueryString["option"];
                goods gs = new goods();
                goodsManager gsManager = new goodsManager();
                goodsflowing gf = new goodsflowing();
                goodsflowingManager gfManager = new goodsflowingManager();
                string goodsName = fc["goodsName"];
                string finalCost = fc["finalCost"];
                string price = fc["price"];
                string oldAmount = fc["oldAmount"];
                string addAmount = fc["addAmount"];
                string goodsId = fc["goodsId"];
                if (!string.IsNullOrEmpty(goodsId))
                {
                    if (!string.IsNullOrEmpty(option))
                    {
                        if (Convert.ToInt32(option) == 0)
                        {
                            int add = Convert.ToInt32(addAmount);
                            if (add > 0)
                            {
                                int newAmount = Convert.ToInt32(oldAmount) + Convert.ToInt32(addAmount);
                                gs = gsManager.getBygoodsId(goodsId);
                                gs.amount = newAmount;
                                gsManager.Update(); 
                                string name = System.Web.HttpContext.Current.User.Identity.Name;
                                gf.username = name;
                                gf.Time = DateTime.Now;
                                gf.operate = "商品进货";
                                gf.goodsId = gs.goodsId;
                                gf.goodsName = gs.goodsName;
                                gf.unit = gs.unit;
                                gf.price = Convert.ToDecimal(gs.price);
                                gf.finalCost = Convert.ToDecimal(gs.finalCost);
                                gf.amount = add;
                                gf.goodsTypeId = gs.goodsTypeId;
                                gfManager.Add(gf);
                                gfManager.Update();
                                return Content("<script>alert('进货完成');window.parent.location.href='/Produce/ProduceInfo';parent.layer.close(parent.layer.getFrameIndex());</script>");
                            }
                        }
                        else if (Convert.ToInt32(option) == 1)
                        {
                            int add = Convert.ToInt32(addAmount);
                            if (add > 0)
                            {
                                if (Convert.ToInt32(oldAmount) < add)
                                {
                                    return Content("<script>alert('库存不足,退货失败');window.parent.location.href='/Produce/ProduceInfo';parent.layer.close(parent.layer.getFrameIndex());</script>");
                                }
                                int newAmount = Convert.ToInt32(oldAmount) - Convert.ToInt32(addAmount);
                                gs = gsManager.getBygoodsId(goodsId);
                                gs.amount = newAmount;
                                gsManager.Update();
                                string name = System.Web.HttpContext.Current.User.Identity.Name;
                                gf.username = name;
                                gf.Time = DateTime.Now;
                                gf.operate = "商品退货";
                                gf.goodsId = gs.goodsId;
                                gf.goodsName = gs.goodsName;
                                gf.unit = gs.unit;
                                gf.price = Convert.ToDecimal(gs.price);
                                gf.finalCost = Convert.ToDecimal(gs.finalCost);
                                gf.amount = add;
                                gf.goodsTypeId = gs.goodsTypeId;
                                gfManager.Add(gf);
                                gfManager.Update();
                                return Content("<script>alert('退货完成');window.parent.location.href='/Produce/ProduceInfo';parent.layer.close(parent.layer.getFrameIndex());</script>");
                            }
                        }
                    }
                }
            }
            else
            {
                return Content("<script>alert('已经超时,请重新登录');window.parent.location.href='/Login/Login';</script>");
            }
            return View();
        }


        // 导出
        
        public ActionResult ProduceToExcel( FormCollection fc)
        {
            goods gs = new goods();
            goodsManager gsManager = new goodsManager();
            goodsType gt = new goodsType();
            goodsTypeManager gtManager = new goodsTypeManager();
            List<goods> gsList = gsManager.GetAll().ToList();
            for (int i = 0; i < gsList.Count(); i++)
            {
                gsList[i].goodsTypeId = gtManager.GetByID(Convert.ToInt32(gsList[i].goodsTypeId)).typeName;
            }
            DataTable dt = LINQToDataTable(gsList);
            string fileName = "goods" + DateTime.Now.ToShortDateString().Replace("_", "-") + ".xls";
            CreateExcel(dt, fileName);
            return View();
        }
        public void CreateExcel(DataTable dt, string fileName)
        {
            HttpResponse resp = System.Web.HttpContext.Current.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            string colHeaders = "", ls_item = "";
            ////定义表对象与行对象，同时用DataSet对其值进行初始化
            //DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
            int i = 0;
            int cl = dt.Columns.Count;

            //取得数据表各列标题，各标题之间以t分割，最后一个列标题后加回车符
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列，加n
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                }
            }
            resp.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息
            //逐行处理数据 
            foreach (DataRow row in myRow)
            {
                //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据   
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列，加n
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }
                }
                resp.Write(ls_item);
                ls_item = "";
            }
            resp.End();

        }
        //将泛型转化成dataTable类型
        public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {   //定义要返回的DataTable对象
            DataTable dtReturn = new DataTable();
            // 保存列集合的属性信息数组
            PropertyInfo[] oProps = null;
            if (varlist == null) return dtReturn;//安全性检查
            //循环遍历集合，使用反射获取类型的属性信息
            foreach (T rec in varlist)
            {
                //使用反射获取T类型的属性信息，返回一个PropertyInfo类型的集合
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    //循环PropertyInfo数组
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;//得到属性的类型
                        //如果属性为泛型类型
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                        == typeof(Nullable<>)))
                        {   //获取泛型类型的参数
                            colType = colType.GetGenericArguments()[0];
                        }
                        //将类型的属性名称与属性类型作为DataTable的列数据
                        if (pi.Name == "goodsId")
                        {
                            dtReturn.Columns.Add(new DataColumn("商品编号", colType));
                        }
                        if (pi.Name == "goodsName")
                        {
                            dtReturn.Columns.Add(new DataColumn("商品名称", colType));
                        }
                        if (pi.Name == "unit")
                        {
                            dtReturn.Columns.Add(new DataColumn("计量单位", colType));
                        }
                        if (pi.Name == "price")
                        {
                            dtReturn.Columns.Add(new DataColumn("商品售价", colType));
                        }
                        if (pi.Name == "Promotionprice")
                        {
                            dtReturn.Columns.Add(new DataColumn("促销售价", colType));
                        }
                        if (pi.Name == "finalCost")
                        {
                            dtReturn.Columns.Add(new DataColumn("商品成本", colType));
                        }
                        if (pi.Name == "amount")
                        {
                            dtReturn.Columns.Add(new DataColumn("商品数量", colType));
                        }
                        if (pi.Name == "goodsTypeId")
                        {
                            dtReturn.Columns.Add(new DataColumn("商品类别", colType));
                        }
                        if (pi.Name == "Promotion")
                        {
                            dtReturn.Columns.Add(new DataColumn("是否促销",colType ));
                        }
                         
                    }
                }
                //新建一个用于添加到DataTable中的DataRow对象
                DataRow dr = dtReturn.NewRow();
                //循环遍历属性集合
                foreach (PropertyInfo pi in oProps)
                {   //为DataRow中的指定列赋值

                    if (pi.Name == "goodsId")
                    {
                        dr["商品编号"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "goodsName")
                    {
                        dr["商品名称"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "unit")
                    {
                        dr["计量单位"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "price")
                    {
                        dr["商品售价"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "Promotionprice")
                    {
                        dr["促销售价"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "finalCost")
                    {
                        dr["商品成本"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "amount")
                    {
                        dr["商品数量"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "goodsTypeId")
                    {
                        dr["商品类别"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "Promotion")
                    {
                        //if (pi.GetValue(rec, null) != null)
                        //{
                        //    if (Convert.ToBoolean(pi.GetValue(rec, null)) == true)
                        //    {
                        //        dr["是否促销"] = "是";
                        //    }
                        //    else
                        //    {
                        //        if (Convert.ToBoolean(pi.GetValue(rec, null)) == false)
                        //        {
                        //            dr["是否促销"] = "否";
                        //        }
                        //    }
                        //}
                        //else
                        //{
                        //    dr["是否促销"] = DBNull.Value;
                        //}
                        dr["是否促销"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    
                }
                //将具有结果值的DataRow添加到DataTable集合中
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;//返回DataTable对象
        }
    }
}
